<template>
  <view class="companyCode">
    <image :src="app.logo" class="logo"></image>
    <u-form ref="uForm" :model="form" label-width="160">
      <u-form-item :label="$t('公司编码')" prop="orgCode">
        <u-input v-model="form.orgCode" />
      </u-form-item>
    </u-form>
    <u-button
      :custom-style="customStyle"
      :loading="loading"
      :ripple="true"
      ripple-bg-color="#55aaff"
      shape="circle"
      type="success"
      @click="bindReg()"
    >
      {{$t('下一步')}}
    </u-button>
    <u-no-network></u-no-network>
  </view>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  data() {
    return {
      fromName: '',
      loading: false,
      customStyle: {
        background: 'linear-gradient(to right, #00aaff 0%,#0079FF 100%)',
        margin: '80rpx 0 20rpx'
      },
      form: {
        orgCode: ''
      },
      rules: {
        orgCode: [
          {
            required: true,
            message: this.$t('请输入公司编码'),
            trigger: ['blur']
          }
        ]
      }
    }
  },
  computed: mapGetters(['userInfo', 'api', 'app']),
  onReady() {
    this.$refs.uForm.setRules(this.rules)
  },
  onLoad(e) {
    uni.setNavigationBarTitle({ title: this.$t('公司编码') })
    this.fromName = e.from
  },
  onShow() {},
  methods: {
    bindReg() {
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.fromName === '注册') {
            this.sever_url('../register/register')
          }
          if (this.fromName === '忘记密码') {
            this.sever_url('../pwd/pwd')
          }
        }
      })
    },
    sever_url(navigate_url) {
      this.$u.get(this.api.GLOBAL_auth_org_dir, { orgCode: this.form.orgCode }).then((res) => {
        this.loading = false
        if (res.code === 200 && res.data) {
          uni.navigateTo({
            url: navigate_url + '?orgCode=' + res.data.code
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.companyCode {
  background-color: #fff;
  padding: 50upx;
  box-sizing: border-box;
  height: 100%;

  .logo {
    width: 250upx;
    height: 250upx;
    margin: 0 auto 20upx;
    display: block;
  }
}
</style>
